{% extends "base.html" %}

{% block content %}
<script type="text/javascript" src="/static/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.min.js"></script>
<script type="text/javascript" src="/static/js/artDialog/jquery.artDialog.js?skin=default"></script>
<script type="text/javascript" src="/static/js/artDialog/plugins/iframeTools.js"></script>
<form action='' method='post'>
{{ttt}}
<table aligh="center" width="100%" class="aui-ibox-data aui-dotted-split">
	<tr>
		<td></td>
		<td colspan="5" align="left"><h1>Send Mail</h1></td>
	</tr>
	<tr>
		<td width="100px" align="right">mail to:</td>
		<td >&nbsp;
			<input type='text' class="text" name='sendto' readonly id="sendto" value="Select Mail List" size="40">
			<input type='hidden' name='maillist_id' id="maillist_id" value="">
			<input type='hidden' name='gmid' id="gmid" value="">
			<input type='hidden' name='letid' id="letid" value="">
			<input type='hidden' name='logid' id="logid" value="">
			<input type='hidden' name='newletterid' id="newletterid" value="">
			&nbsp;
			[<a href="javascript:void(0)" id='select_mail'>Select Mail List</a>]&nbsp;&nbsp;[<a href="javascript:void(0)" id='del_mail'>Clears The Selection</a>]
		</td>
	</tr>
	<tr>
		<td width="100px" align="right">mailletter name:</td>
		<td >&nbsp;
			<input type='text' name='lettername' class="text" id="lettername" size="40">
			&nbsp;
			[<a href="javascript:void(0)" id='select_letter'>Choose for record</a>]
		</td>
	</tr>
	<tr>
		<td width="100px" align="right">mail subject:</td>
		<td >&nbsp;
			<input type='text' name='object' class="text" id="object" size="40">
		</td>
	</tr>
	<tr>
		<td width="100px" align="right" valign="center">content：</td>
		<td colspan='10'>
			<div id="edit_content">
				<INPUT id=content style="DISPLAY: none" type=hidden name="modcontent" value="">
				<INPUT id=content___Config style="DISPLAY: none" type=hidden> 
				<IFRAME id=content___Frame src="/medias/ckeditor/static/editor/editor/fckeditor.html?InstanceName=modcontent&Toolbar=Default" frameBorder=0 width=1000 scrolling=no height=500> 
				</IFRAME>
			</div>
			<div id="letter_con" style="display:none;"></div>
		</td>
	</tr>
	<tr>
		<td></td>
		<td>
			<input class="g-button g-button-submit" type='button' name='sendMail' value='发送'/>
			<!--<input class="g-button g-button-submit look_con" type='button' name='watch' value='预览'/>-->
		</td>
	</tr>
</table>
</form>
<div id="dialog"></div>
<script>
	function runCode(url,content) //定义一个运行代码的函数，
	{
		// var code=event.srcElement.parentElement.children[0].value;//即要运行的代码。
		var newwin=window.open(url); //打开一个窗口并赋给变量newwin。
		newwin.opener = null // 防止代码对论谈页面修改
		newwin.document.write(content); //向这个打开的窗口中写入代码code，这样就实现了运行代码功能。
		newwin.document.close();
	} 
	$('input.look_con').click(function(){
		var conname = FCKeditorAPI.GetInstance('modcontent')
		var con = conname.EditorDocument.body.innerHTML
		var url = ''
		runCode(url,con)
	// window.open("/www/content/index/modid/"+$('input[name="modid"]').val()+"/content/"+escape(con))
	}) 
	$('a#select_letter').click(function(){
		art.dialog.data('letterid',$('#newletterid').val())
		art.dialog.open('/gletterlist', {title: 'Letter List',width:'80%',height:470});
	})
	$('a#select_mail').click(
		function (){
			selectUser();
		}
	)
	$('a#del_mail').click(function(){
		$("input[name='sendto']").val('Select Mail List')
		$('input#maillist_id').val('')
		art('#del_mail').dialog({content:'Clear Success!',ok:true})
	})
	$("input[name='sendto']").focus(function(){
		if($(this).val() == 'Select Mail List'){
			selectUser();
		}
	})
	function selectUser(){
		art.dialog.data('maillistid',$('#maillist_id').val())
		art.dialog.open('/gmaillist', {title: 'Mail List',width:'80%',height:470});
	}
	$('input[name="sendMail"]').click(function(){
		var maillistid = $('input#maillist_id').val()
		var obj = $('input#object').val()
		var mail = FCKeditorAPI.GetInstance('modcontent')
		var mailcon = mail.EditorDocument.body.innerHTML
		var lettername = $('input[name="lettername"]').val()
		var gmid = $('input[name="gmid"]').val()
		var letid = $('input[name="letid"]').val()
		var logid = $('input[name="logid"]').val()
		var newletterid = $('input[name="newletterid"]').val()

		if(maillistid==''){
			art.dialog.alert('Please first choice E-mail recipient')
			return false;
		}
		if(obj==''){
			art.dialog.alert("The mail subject can't for empty!")
			return false;
		}
		if(newletterid=='' && (mailcon=='&nbsp;' || mailcon.replace(/<[^>].*?>/g,"")=='')){
			art.dialog.alert("Mail contents can't be empty!")
			return false;
		}
		var url = '/gsendmail/'
		var data = {'maillistid':maillistid,'lettername':lettername,'gmid':gmid,'object':obj,'mailcon':mailcon,'host':location.host,'letid':letid,'logid':logid,'newletterid':newletterid}
//		$.post(url,data,function(e,code){
//			if(code == 'success'){
//				var user = JSON.parse(e);
//			}
//		})
		var dialog = art.dialog({id: 'N3690',title:'Sending ...',lock:true});
		$.ajax({
			url:url,
			data:data,
			type:'post',
			success:(function(e,code){
				if(code == 'success'){
					var user = JSON.parse(e);
					if(user['code']==1){
						dialog.title('Successful!')
						dialog.content(user['info']);
						dialog.button({name:'OK',callback:(function(){window.location.reload();})})
						$('#letid').val('')
						$('#logid').val('')
						$('#newletterid').val('')
					}else{
						dialog.title('ERROR!')
						dialog.content(user['info']);
						dialog.button({name:'close'})
						if(user['ids']!=undefined && user['ids']!=''){
							var ids = user['ids'].split(',')
							if (ids.length>0){
								$('#letid').val(ids[0])
								$('#logid').val(ids[1])
								if (ids[2]==0){
									$('#newletterid').val('')
								}else{
									$('#newletterid').val(ids[2])
								}
								
							}
						}
					}
				}
			})
		})
	})
</script>
{% endblock %}